package tools.hmmer;

import java.io.BufferedReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import tools.parents.Parser;


public class hmmerParser extends Parser{

//	private BufferedReader in;
	private hmmerAlignment currAlign=null;
	private hmmerAlignment nextAlign=null;
//	private boolean hasMore;
	private String type;
	
	public hmmerParser(BufferedReader reader)throws Exception{
		super(reader);
		String s=reader.readLine();
		if(s.startsWith("hmmpfam")){
			type="hmmpfam";
			readNext();
		}else{
			throw new Exception("Unknown line "+s);
		}
	}
	public hmmerAlignment nextHit() throws Exception{
		if (!hasMore) {
			throw new Exception("No more hits");
		}
		currAlign= nextAlign;
		readNext();
		return currAlign;
	}
	public hmmerAlignment getHit() throws Exception{
		if(!hasMore)
			throw new Exception("Next hit is nada");
		return currAlign;
	}
	public boolean hasMore(){
		return hasMore;
	}
	
	private void readNext()throws Exception{
		String s=reader.readLine();
		for(;s!=null;s=reader.readLine())
			if(s.startsWith("Query sequence: ")){
				break;
			}
		if(s==null){
			hasMore=false;
		}else{
			String qname=s.substring(16);
			s=reader.readLine();
			String qaccession=s.substring(16);
			s=reader.readLine();
			String qdescription=s.substring(16);
			reader.readLine();
			reader.readLine();
			reader.readLine();
			reader.readLine();
			Hashtable<String, Integer> ht=new Hashtable<String, Integer>();
			Vector<hmmerMultipleModelAlignment> v=new Vector<hmmerMultipleModelAlignment>();
			int size=0,pos=0;
			s=reader.readLine();
			hmmerAlignment tmpAlign;
			if(s.equals("	[no hits above thresholds]")){
				tmpAlign=new hmmerAlignment(0,0);
				for(s=reader.readLine(),pos=0;!s.startsWith("//");s=reader.readLine());
			}else{
				for(;s.length()>0;s=reader.readLine()){
					String[] l=s.substring(53).split(" +");
					int top_size=Integer.parseInt(l[3]);
					size+=top_size;
					hmmerMultipleModelAlignment tmp=new hmmerMultipleModelAlignment(size);
					tmp.hmmname=s.split(" +")[0];
					tmp.hmmdescription=s.substring(16,54);
					tmp.score=Double.parseDouble(l[1]);
					tmp.eValue=Double.parseDouble(l[2]);
					ht.put(tmp.hmmname,new Integer(pos));
					v.add(pos++,tmp);
				}
				tmpAlign=new hmmerAlignment(v.size(),size);
				for(int i=0;i<v.size();i++)
					tmpAlign.hmm_top[i]=(hmmerMultipleModelAlignment)v.get(i);
				reader.readLine();
				reader.readLine();
				reader.readLine();
				for(s=reader.readLine(),pos=0;!s.startsWith("//");s=reader.readLine(),pos++){
					String[] l=s.split(" +");
					hmmerModelAlignment tmp=new hmmerModelAlignment();
					tmp.hmmname=l[0];
					tmp.tstart=Integer.parseInt(l[2]);
					tmp.tend=Integer.parseInt(l[3]);
					tmp.tProp[0]=l[4].charAt(0);
					tmp.tProp[1]=l[4].charAt(1);
					tmp.hmmstart=Integer.parseInt(l[5]);
					tmp.hmmend=Integer.parseInt(l[6]);
					tmp.hmmProp[0]=l[7].charAt(0);
					tmp.hmmProp[1]=l[7].charAt(1);
					tmp.score=Double.parseDouble(l[8]);
					tmp.eValue=Double.parseDouble(l[9]);
					int top_pos=Integer.parseInt(l[1].split("/")[0]);
					tmpAlign.hmm_top[((Integer)ht.get(tmp.hmmname)).intValue()].hmmerModelAlignments[top_pos-1]=tmp;
					tmpAlign.hmms[pos]=tmp;
				}
			}
			nextAlign=tmpAlign;
			nextAlign.qname=qname;
			nextAlign.qaccession=qaccession;
			nextAlign.qdescription=qdescription;
			hasMore=true;
		}
	}
}
